//
//  AppDelegate+OpenInstall.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/9/10.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "AppDelegate+OpenInstall.h"
#import <AdSupport/AdSupport.h>
#import <libOpenInstallSDK/OpenInstallSDK.h>
#import <AppTrackingTransparency/AppTrackingTransparency.h>
@implementation AppDelegate (OpenInstall) 
- (void)initOpenInstall {
    // 在Info.plist文件中配置appKey键值对
    /**
     https://developer.openinstall.io/1191375335/app-ios
     <key>com.openinstall.APP_KEY</key>
     <string>jpbb3b</string>
     渠道分析 裂变分享 暂时未细致开发
     */
    NSUUID * uuid = [[ASIdentifierManager sharedManager]advertisingIdentifier];
    [OpenInstallSDK initWithDelegate:self advertisingId:uuid.UUIDString];
    [[OpenInstallSDK defaultManager]getInstallParmsWithTimeoutInterval:40 completed:^(OpeninstallData * _Nullable appData) {
        if (appData.opCode == OPCode_timeout) {
            //初始化超时，需要进行重试获取安装参
        } else {
            if ([appData.data isKindOfClass:[NSDictionary class]]) {
                //(动态安装参数)
                [TTUserModel sharedObject].installModel = [TTInstallModel modelWithDictionary:appData.data];
            }
            if (appData.channelCode) {
                //(通过渠道链接或二维码安装会返回渠道编号)
                //e.g.可自己统计渠道相关数据等
            }
        }
        NSLog(@"OpenInstallSDK:\n动态参数：%@;\n渠道编号：%@",appData.data,appData.channelCode);
    }];
}
- (BOOL)application_appInstall:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
    // userActivity.referrerURL https://app-jpbb3b.openinstall.com/
    // userActivity.webpageURL https://jpbb3b.openinstall.io/ulink/c/eyJkIjp7InRlc3RLZXkiOiJ0ZXN0VmFsdWUifSwibSI6Imt3Sm9RRjZsTG5FQUFBR1I0ZTZsd1VMT0lqdUk5NXN5ZnMxOG4tQ0tHM2l5cktiTUZyd1kyRnhnNk9QRWFzOEZtSkhHNzU0MUg0M0wifQ==
    return [OpenInstallSDK continueUserActivity:userActivity];
}
- (BOOL)application_appInstall:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    //处理通过openinstall URL scheme拉起App的数据
    return [OpenInstallSDK handLinkURL:url];
}
//通过OpenInstall获取已经安装App被拉起时的参数（如果是通过渠道页面拉起App时，会返回渠道编号）
- (void)getWakeUpParams:(OpeninstallData *)appData {
    if (appData.data) {//(获取自定义参数)
        //e.g.如免填邀请码建立邀请关系、自动加好友、自动进入某个群组或房间等
    }
    if (appData.channelCode) {//(获取渠道编号参数)
        //e.g.可自己统计渠道相关数据等
    }
    NSLog(@"OpenInstallSDK:\n动态参数：%@;\n渠道编号：%@",appData.data,appData.channelCode);
}
@end
